/*
 * graph2D
 * Copyright (c) 2009 Shun Moriya <shun@mnu.sakura.ne.jp>
 *
 * The MIT License
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#if !defined(___GRAPH2D_IMAGE_BUTTON_H___)
#define ___GRAPH2D_IMAGE_BUTTON_H___

#include "container.h"
#include <string>

namespace Graph2D
{
	class ImageButton : public Component
	{
		DISALLOW_COPY_AND_ASSIGN(ImageButton);

		typedef Component super;

	public:
		typedef enum Direction
		{
			DIRECTION_CENTER = 0,
			DIRECTION_LEFT,
			DIRECTION_RIGHT,
		}Direction;

	public:
		ImageButton();
		virtual ~ImageButton(){}

		virtual const char* getTypeName() const
		{
			return "ImageButton";
		}

		float getWidth() const;
		void setWidth(const float width);

		bool loadFrameTexture(const Direction direction, const std::string& filename, const bool pushed);
		void unloadFrameTexture(const Direction direction, const bool pushed);

		const Vector2& getFrameTexCoord(const Direction direction, const bool pushed) const;
		void setFrameTexCoord(const Direction direction, const Vector2& frameTexCoord, const bool pushed);
		void setFrameTexCoord(const Direction direction, const float u, const float v, const bool pushed);

		//! 四隅フレームテクスチャサイズ
		float getLeftFrameSize() const
		{
			return leftFrameSize;
		}

		void setLeftFrameSize(const float size)
		{
			leftFrameSize = size;
		}

		float getRightFrameSize() const
		{
			return rightFrameSize;
		}

		void setRightFrameSize(const float size)
		{
			rightFrameSize = size;
		}

		void setClickEvent(ClickEvent* clickEvent)
		{
			this->clickEvent = clickEvent;
		}

		virtual void onSerialize(mana_stream* stream) const;
		virtual void onDeserialize(mana_stream* stream);
		virtual bool onDraw(const DrawRect& drawRect);
		virtual bool onTouchesBegan(const Vector2& localPosition);
		virtual bool onTouchesEnded(const Vector2& localPosition);
		virtual bool onTouchesCancelled(const Vector2& localPosition);

		virtual bool compare(const ImageButton& other) const;

	private:
		void drawFrame(Texture* texture, const Vector2& uv, const Vector2& v0, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Color& color, const float width, const float height);

	private:
		Texture* frameTexture[3][2];
		Vector2 frameTexCoord[3][2];
		float leftFrameSize;
		float rightFrameSize;
		ClickEvent* clickEvent;
	};
}

#endif
